<template>
  <div>
    <el-container>
      <!--      <el-aside width="5%"></el-aside>-->
      <el-main class="register-main"
               style="margin-bottom: 10px;width: 400px;height:100%;vertical-align: center">
        <h1 style="color: #409EFF;text-align: center">编辑义工信息</h1>

        <img :src="getVolunteerUrl" style="width:200px;height:200px;margin-left: 250px">

        <el-form :model="volunteer" :rules="rules" label-width="20%">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="volunteer.name" type="text" autocomplete="off" placeholder="请输入姓名" clearable
                      size="medium" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="性别" required>
            <el-select v-model="gender" placeholder="请选择性别" style="width: 90%">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="电话" prop="phone" required>
            <el-input v-model="volunteer.phone" type="tel" autocomplete="off" placeholder="请输入电话" clearable
                      size="medium" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="身份证号" required>
            <el-input v-model="volunteer.idCard" type="email" autocomplete="off" placeholder="请输入身份证号" clearable
                      size="medium" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="生日" required>
            <el-input v-model="volunteer.birthday" type="date" autocomplete="off" placeholder="请输入生日日期" clearable
                      size="medium" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="入院日期" required>
            <el-input v-model="volunteer.checkinDate" type="date" autocomplete="off" placeholder="请输入入院日期" clearable
                      size="medium" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="离院日期" required>
            <el-input v-model="volunteer.checkoutDate" type="date" autocomplete="off" placeholder="请输入离院日期" clearable
                      size="medium" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="描述" required>
            <el-input v-model="volunteer.description" type="text" autocomplete="off" placeholder="请输入义工的描述" clearable
                      size="medium" style="width: 90%"></el-input>
          </el-form-item>

          <el-form-item label-width="0">
            <el-button type="primary" @click="save()" style="margin-left: 20%;width:100px">保存</el-button>
            <el-button type="warning" @click="reload()" style="margin-left: 20%;width:100px">重置</el-button>
            <div class="clearBox"></div>
          </el-form-item>
        </el-form>
      </el-main>
      <!--      <el-aside width="5%"></el-aside>-->
    </el-container>
  </div>
</template>

<script>
export default {
  name: "EditVolunteer",
  data() {
    return {
      volunteer: {
        id:"",
        name: "",
        gender:"",
        idCard: "",
        phone: "",
        birthday:"",
        checkinDate:"",
        checkoutDate:"",
        description:"",
        imgsetDir:"",
        profilePhoto:"",
      },
      gender:"",
      options: [{
        value: '选项1',
        label: '男'
      }, {
        value: '选项2',
        label: '女',
      }, {
        value: '选项3',
        label: '其他',
      }],
      }
  },
  watch: {
    gender(newName){
      if(newName == "选项1"){
        this.volunteer.gender = "男";
      }
      else if(newName == "选项2") {
        this.volunteer.gender = "女";
        //print(111);
      }
      else if(newName == "选项3") {
        this.volunteer.gender = "其他";
        //print(111);
      }
    }
  },
  computed: {
    getVolunteerUrl: function(){
      if(localStorage.getItem('volunteerUrl') == "null" || localStorage.getItem('volunteerUrl') == null || localStorage.getItem('volunteerUrl') == "") {
        return "https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png";
      }
      else{
        return localStorage.getItem('volunteerUrl');
      }
    }
  },
  created() {
    if (this.$route.params.isReload == 'true') {
      this.$router.go(0);
    }
    this.getVolunteerInfo();
  },
  methods: {
    save: function(){

      const _this = this;
      //alert(_this.volunteer.gender)
      this.$axios.post('/volunteer/update',_this.volunteer, {
        headers: {
          "Content-Type": "application/json;charset=utf-8"
        },
        withCredentials: true
      }).then(function (/*response*/) {
        // 这里是处理正确的回调
        _this.$message({
          message: '保存成功',
          type: 'success'
        });
        _this.$router.go(0);
      }).catch(function (response) {
        // 这里是处理错误的回调
        _this.$message.error(response.data.message)
      })
    },
    handleClose(done) {
      done();
    },
    reload: function(){
      this.$router.go(0);
      this.getVolunteerInfo()
    },
    getVolunteerInfo: function(){
      this.volunteer.id = localStorage.getItem('volunteerId');
      this.volunteer.name = localStorage.getItem('volunteerName');
      this.volunteer.gender = localStorage.getItem('volunteerGender');
      this.gender = this.volunteer.gender;
      this.volunteer.phone = localStorage.getItem('volunteerPhone');
      this.volunteer.idCard = localStorage.getItem('volunteerIdCard');
      this.volunteer.birthday = localStorage.getItem('volunteerBirthday');
      this.volunteer.checkinDate = localStorage.getItem('volunteerCheckInDate');
      this.volunteer.checkoutDate = localStorage.getItem('volunteerCheckOutDate');
      this.volunteer.imgsetDir = localStorage.getItem('volunteerImgsetDir');
      this.volunteer.profilePhoto = localStorage.getItem('volunteerProfilePhoto');
      this.volunteer.description = localStorage.getItem('volunteerDescription');
    },
  }

}
</script>

<style scoped>

</style>
